package com.haoshuang.zhouzhoubang.ui.page.mine.invite.record

import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.FragmentInviteTeamMemberRecordChildBinding
import com.haoshuang.zhouzhoubang.model.bean.SpreadServiceRecordBean
import com.haoshuang.zhouzhoubang.ui.dialog.ChooseDateRangeDialog
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.adapter
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.vertical
import com.mufeng.libs.utils.view.visible

/**
 *  
 */
class InviteTeamMemberRecordChildFragment : BaseVMFragment<InviteTeamMemberRecordChildViewModel, FragmentInviteTeamMemberRecordChildBinding>() {

    companion object {
        fun getInstance(type: Int): InviteTeamMemberRecordChildFragment {
            val fragment = InviteTeamMemberRecordChildFragment()
            fragment.arguments = Bundle().apply {
                putInt("type", type)
            }
            return fragment
        }
    }

    private val type: Int by lazy {
        arguments?.getInt("type") ?: 0
    }

    /**
     * 1: 今天, 2: 本周, 3: 本月, 4: 本年 0: 自定义
     */
    private var timeType = 1

    private var start_time = ""
    private var end_time = ""

    private val data = arrayListOf<SpreadServiceRecordBean>()
    private val adapter = InviteTeamMemberRecordAdapter(data)

    override fun initView(savedInstanceState: Bundle?) {

        binding.apply{

            binding.rgTime.setOnCheckedChangeListener { group, checkedId ->
                binding.tvCustomTime.gone()
                start_time = ""
                end_time = ""
                when (checkedId) {
                    binding.rbToday.id -> {
                        if (binding.rbToday.isChecked) {
                            timeType = 1
                            p = 1
                            vm.spreadServiceRecord(type, timeType, start_time, end_time, p)
                        }
                    }
                    binding.rbCurrentWeek.id -> {
                        if (binding.rbCurrentWeek.isChecked) {
                            timeType = 2
                            p = 1
                            vm.spreadServiceRecord(type, timeType, start_time, end_time, p)
                        }
                    }
                    binding.rbCurrentMonth.id -> {
                        if (binding.rbCurrentMonth.isChecked) {
                            timeType = 3
                            p = 1
                            vm.spreadServiceRecord(type, timeType, start_time, end_time, p)
                        }
                    }
                    binding.rbCurrentYear.id -> {
                        if (binding.rbCurrentYear.isChecked) {
                            timeType = 4
                            p = 1
                            vm.spreadServiceRecord(type, timeType, start_time, end_time, p)
                        }
                    }
                    binding.rbCustomTime.id -> {
                        if (binding.rbCustomTime.isChecked) {
                            timeType = 0
                            start_time = ""
                            end_time = ""
                            binding.tvCustomTime.visible()
                            binding.tvCustomTime.text = "请选择查询时间段"
                            XPopup.Builder(mActivity)
                                .asCustom(ChooseDateRangeDialog(mActivity){ start_date, end_date ->
                                    start_time = start_date
                                    end_time = end_date
                                    binding.tvCustomTime.text = start_date + " ~ " + end_date
                                    p = 1
                                    vm.spreadServiceRecord(type, timeType, start_time, end_time, p)
                                })
                                .show()
                        }else{
                            binding.tvCustomTime.gone()
                        }
                    }
                }
            }

            binding.tvCustomTime.click {
                XPopup.Builder(mActivity)
                    .asCustom(ChooseDateRangeDialog(mActivity){ start_date, end_date ->
                        start_time = start_date
                        end_time = end_date
                        binding.tvCustomTime.text = start_date + " ~ " + end_date
                        p = 1
                        vm.spreadServiceRecord(type, timeType, start_time, end_time, p)
                    })
                    .show()
            }

            binding.recyclerView.vertical().adapter(adapter)
            adapter.setEmptyData()

            refreshLayout.setOnRefreshListener {
                p = 1
                isShowing = false
                vm.spreadServiceRecord(type, timeType, start_time, end_time, p)
            }

            refreshLayout.setOnLoadMoreListener {
                p++
                isShowing = false
                vm.spreadServiceRecord(type, timeType, start_time, end_time, p)
            }
        }

    }

    override fun initData() {

    }

    override fun lazyLoadData() {
        p = 1
        vm.spreadServiceRecord(type, timeType, start_time, end_time, p)
    }

    override fun startObserve() {
        vm.spreadServiceRecordData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupEmptyData(data, adapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it, data, adapter, p)
            }
        }
    }

}